package com.zankong.tool.zkappforas.div_item;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.zankong.tool.zkapp.document.ZKDocument;
import com.zankong.tool.zkapp.item.HolderInit;
import com.zankong.tool.zkapp.item.ZKAdapter;
import com.zankong.tool.zkapp.util.ZKAppAdapter;
import com.zankong.tool.zkappforas.R;

import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.HashMap;

/**
 * Created by YF on 2018/9/17.
 */

@ZKAppAdapter("task_release")
public class Task_Release extends ZKAdapter {
    private final int STATUS_RELEASED = 0;
    private final int STATUS_RUNNING = 1;
    private final int STATUS_FINISH = 2;
    private HashMap<String,Integer> mStatus = new HashMap<String,Integer>(){{
        put("released",STATUS_RELEASED);
        put("running",STATUS_RUNNING);
        put("finish",STATUS_FINISH);
    }};
    private HashMap<String,String> mClickMap = new HashMap<>();

    public Task_Release(ZKDocument zKDocument, Element element) {
        super(zKDocument, element);
        for (Attribute attribute : element.element("task_release").attributes()) {
            mClickMap.put(attribute.getName(),attribute.getValue());
        }
    }

    @Override
    protected RecyclerView.ViewHolder onCreate(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder holder = null;
        switch (i){
            case STATUS_RELEASED:
                holder = new ReleasedHolder(mLayoutInflater.inflate(R.layout.item_task_release_released,viewGroup,false));
                break;
            case STATUS_RUNNING:
                holder = new RunningHolder(mLayoutInflater.inflate(R.layout.item_task_release_running,viewGroup,false));
                break;
            case STATUS_FINISH:
                holder = new FinishHolder(mLayoutInflater.inflate(R.layout.item_task_release_finish,viewGroup,false));
                break;
        }
        return holder;
    }

    @Override
    protected int getVIewType(int position) {
        return mStatus.get(mList.get(position).getString("status"));
    }

    @Override
    protected void onBind(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof HolderInit) {
            ((HolderInit) viewHolder).init(position);
        }
    }

    private class ReleasedHolder extends RecyclerView.ViewHolder implements HolderInit {
        Button change,cancel,receiver;
        public ReleasedHolder(@NonNull View itemView) {
            super(itemView);
            change = itemView.findViewById(R.id.change);
            cancel = itemView.findViewById(R.id.cancel);
            receiver = itemView.findViewById(R.id.receiver);
        }

        @Override
        public void init(int position) {
            change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("change")){
                        invokeWithContext(mClickMap.get("change"),ReleasedHolder.this);
                    }
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("cancel")){
                        invokeWithContext(mClickMap.get("cancel"),ReleasedHolder.this);
                    }
                }
            });
            receiver.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("receiver")) {
                        invokeWithContext(mClickMap.get("receiver"),ReleasedHolder.this);
                    }
                }
            });
        }
    }
    private class RunningHolder extends RecyclerView.ViewHolder implements HolderInit {
        Button cancel,finish,chat;
        public RunningHolder(@NonNull View itemView) {
            super(itemView);
            cancel = itemView.findViewById(R.id.cancel);
            finish = itemView.findViewById(R.id.finish);
            chat = itemView.findViewById(R.id.chat);
        }

        @Override
        public void init(int position) {
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("cancel")){
                        invokeWithContext(mClickMap.get("cancel"),RunningHolder.this);
                    }
                }
            });
            finish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("finish")) {
                        invokeWithContext(mClickMap.get("finish"),RunningHolder.this);
                    }
                }
            });
            chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("chat")) {
                        invokeWithContext(mClickMap.get("chat"),RunningHolder.this);
                    }
                }
            });
        }
    }
    private class FinishHolder extends RecyclerView.ViewHolder implements HolderInit {
        Button drawbill;
        public FinishHolder(@NonNull View itemView) {
            super(itemView);
            drawbill = itemView.findViewById(R.id.drawbill);
        }

        @Override
        public void init(int position) {
            drawbill.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("drawbill")) {
                        invokeWithContext(mClickMap.get("drawbill"),FinishHolder.this);
                    }
                }
            });
        }
    }

}
